package com.csp.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csp.common.utils.R;
import com.csp.main.entity.Order1;
import com.csp.main.service.Order1Service;
import com.csp.main.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/main/order1")
public class Order1Controller {

    @Autowired
    private Order1Service order1Service;


    @GetMapping("/list")
    public R list(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        QueryWrapper<Order1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Order1> list = order1Service.list(queryWrapper);
        return R.ok().data("rows",list);
    }

    @GetMapping("/getById/{orderId}")
    public R getById(@PathVariable String orderId){
        Order1 order1 = order1Service.getById(orderId);
        return R.ok().data("rows",order1);
    }

    @PostMapping("/add/{comId}")
    public R add(HttpServletRequest request,@PathVariable String comId){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        Order1 order1 = new Order1();
        order1.setUserId(userId);
        order1.setComId(comId);
        boolean save = order1Service.save(order1);
        return save?R.ok():R.error();
    }

    @DeleteMapping("/delete/{orderId}")
    public R delete(@PathVariable String orderId){
        boolean b = order1Service.removeById(orderId);
        return b?R.ok():R.error();
    }

    @PutMapping("/update/{orderId}")
    public R update(@PathVariable String orderId){
        Order1 or = order1Service.getById(orderId);
        or.setState(true);
        boolean b = order1Service.updateById(or);
        return b?R.ok():R.error();
    }
}

